package xdTool

import initConfig.SysInitData
import model.SelectData
import sysAuth.SysUsers
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysAuthSQL
import sysUtil.SysCommUtil
import xdInterface.XdController

import java.sql.Clob

class XdWidgetController extends XdController{

    def sysDBUtilService
    def authUtilsService

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        def result = ResultJson.resultJson()

        try {
            def user = SysUsers.findByUserNo(params.userNo)
            def id = Integer.parseInt(params?.id)
            def classFlag = params?.classFlag
            def instance
            switch (classFlag) {
                case "xd-input"://输入框
                    instance = XdWidgetInput.get(id)
                    break;
                case "xd-select"://下拉框
                    instance = XdWidgetSelect.get(id)
                    break;
                case "xd-date"://日期
                    instance = XdWidgetDate.get(id)
                    break;
                case "xd-panel-table"://panel-table
                    instance = XdWidgetPanel.get(id)
                    def tb = XdTableCfg.get(instance.dataId)
                    if(tb){
                        XdTableColCfg.findAllByXdTableId(tb.id).each {
                            it.delete(flush: true)
                        }
                        tb.delete(flush: true)
                    }
                    if(instance){
                        XdWidgetPanelParam.findAllByPanelId(instance.id).each {
                            it.delete(flush: true)
                        }
                    }
                    break;
            }

            def userGrade = authUtilsService.isAvailable(user)
            if(!instance){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else{
                if(instance.creator == user.userNo  &&  SysInitData.USER_GRADE_ADMIN_FUNC.contains(userGrade)){
                    instance.delete(flush: true)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }else{
                    result.put(ResultJson.CODE.name,ResultCode.OPT_NOT_AUTH.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_NOT_AUTH.message)
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def edit() {
        def userNo = params?.userNo
        def classFlag = params?.classFlag
        switch (classFlag) {
            case "xd-input"://输入框
                getOneInput()
                break;
            case "xd-select"://下拉框
                getOneSelect()
                break;
            case "xd-date"://日期
                getOneDate();
                break;
            case "xd-panel-table":
                getOneTable();
                break;
        }
    }


    def getOneTable(){

        def result = ResultJson.resultJson()
        def cols
        try{
            def panelId = Long.parseLong(params?.id)
            def panel =  XdWidgetPanel.get(panelId)
            def baseScript = SysCommUtil.Clob2String(panel.html)
            def table = XdTableCfg?.get(panel?.dataId)
            //println table
            if(table){
                cols = XdTableColCfg.findAll("from XdTableColCfg as a where a.xdTableId = ${table.id}  order by a.grade ,a.colIndex" )
                result.put("cols",SysCommUtil.copyMapList(cols))
                result.put("table",SysCommUtil?.copyMap(table))

            }
            result.put("html","xdWidgetTable.html")
            panel = SysCommUtil.copyMap(panel)
            panel.put("dataSql",panel.dataSql?:"")
            result.put(ResultJson.DATA.name,panel)
            result.put("baseScript",baseScript)

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    def getOneInput(){
        def id = Long.parseLong(params?.id)
        def result = ResultJson.resultJson()
        def data =  XdWidgetInput.get(id)
        def baseScript = SysCommUtil.Clob2String(XdWidget.get(data.xdWidgetId).html)
        result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put("html","xdWidgetInput.html")
        result.put("baseScript",baseScript)
        render result
    }
    def getOneSelect(){
        def id = Long.parseLong(params?.id)
        def result = ResultJson.resultJson()
        def data =  XdWidgetSelect.get(id)
        def baseScript = SysCommUtil.Clob2String(XdWidget.get(data.xdWidgetId).html)
        result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put("html","xdWidgetSelect.html")
        result.put("baseScript",baseScript)
        render result
    }
    def getOneDate(){
        def id = Long.parseLong(params?.id)
        def result = ResultJson.resultJson()
        def data =  XdWidgetDate.get(id)
        def baseScript = SysCommUtil.Clob2String(XdWidget.get(data.xdWidgetId).html)
        result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put("html","xdWidgetDate.html")
        result.put("baseScript",baseScript)
        render result
    }

    @Override
    def save() {
        def userNo = params?.userNo
        def classFlag = params?.classFlag
        switch (classFlag) {
            case "xd-input"://输入框
                saveInput()
                break;
            case "xd-select"://下拉框
                saveSelect()
                break;
            case "xd-date"://日期
                saveDate();
                break;
            case "xd-panel-table":
                savePanel();
                break;
        }
    }

    def savePanel(){
        def result = ResultJson.resultJson()
        try {

            def id = Long.parseLong(params?.id)
            XdWidgetPanel data = XdWidgetPanel.get(id)
            if(!data){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                def newWidget = XdWidgetPanel.findByName(params?.name)
                if(newWidget && newWidget.id != data.id){
                    result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                    result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
                }else{
                    data.setName(params?.name)
                    data.setNameFlag(params?.nameFlag)
                    data.setDataSourceId(params.dataSourceId?Long.parseLong(params.dataSourceId):null  )
                    data.setDataType(params?.dataType)
                    data.setDataSql(SysCommUtil.String2Clob(params?.dataSql))
                    data.setDataPanelId(params.dataPanelId?Long.parseLong(params.dataPanelId):null)
                    data.setHtml(SysCommUtil.String2Clob(params?.html))
                    data.setUpdator(params?.updater)
                    data.save(flush: true)
                    //result.put(ResultJson.DATA.name,params?.html)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }
    def saveInput(){
        def result = ResultJson.resultJson()
        try {

            def id = Long.parseLong(params?.id)
            XdWidgetInput data = XdWidgetInput.get(id)
            if(!data){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                def newWidget = XdWidgetInput.findByName(params?.name)
                if(newWidget && newWidget.id != data.id){
                    result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                    result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
                }else{
                    data.setName(params?.name)
                    data.setLabel(params?.label)
                    data.setPlaceholder(params?.placeholder)
                    data.setUpdator(params?.updater)
                    data.setType(params?.type)
                    data.setVerify(params?.verify)
                    data.setReadable(params?.readable)

                    data.setStyle(params?.style)
                    data.setHtml(SysCommUtil.String2Clob(params?.html))
                    data.save(flush: true)
                    //result.put(ResultJson.DATA.name,params?.html)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }
    def saveSelect(){
        def result = ResultJson.resultJson()
        try {

            def id = Long.parseLong(params?.id)
            XdWidgetSelect data = XdWidgetSelect.get(id)
            if(!data){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                def newWidget = XdWidgetSelect.findByName(params?.name)
                if(newWidget && newWidget.id != data.id){
                    result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                    result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
                }else{
                    data.setName(params?.name)
                    data.setLabel(params?.label)
                    data.setRadio(params.radio?:"")
                    data.setMaxCount(params.maxCount?:"0")
                    data.setOptions(SysCommUtil.String2Clob(params?.options))
                    data.setSkin(params?.skin)
                    data.setDisabled(params.disabled?:"")
                    data.setVerify(params.verify?:"")
                    data.setQuotesStr(params?.quotesStr)
                    data.setSplitStr(params?.splitStr)
                    //data.setEndStr(params?.endStr)
                    data.setWidth(params?.width)
                    data.setServerFlag(params.serverFlag?:"")
                    data.setDataSourceId(params.dataSourceId?Long?.parseLong(params.dataSourceId):null)
                    data.setDataSql(SysCommUtil.String2Clob(params?.dataSql))
                    data.setLabelField(params?.labelField)
                    data.setValueField(params?.valueField)
                    data.setAllFlag(params.allFlag?:"")
                    data.setEvents(params?.events)
                    data.setHtml(SysCommUtil.String2Clob(params?.html))

                    data.setUpdator(params?.updater)
                    data.save(flush: true)
                    //result.put(ResultJson.DATA.name,params?.html)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }
    def saveDate(){
        def result = ResultJson.resultJson()
        try {

            def id = Long.parseLong(params?.id)
            XdWidgetDate data = XdWidgetDate.get(id)
            if(!data){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else {
                def newWidget = XdWidgetDate.findByName(params?.name)
                if(newWidget && newWidget.id != data.id){
                    result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                    result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
                }else{
                    data.setName(params?.name)
                    data.setLabel(params?.label)
                    data.setType(params?.type)
                    data.setVerify(params?.verify)
                    data.setStyle(params?.style)
                    data.setDefaultValue(params?.defaultValue)
                    data.setHtml(SysCommUtil.String2Clob(params?.html))
                    data.setTheme(params.theme?:"default")
                    data.setUpdator(params?.updater)
                    data.save(flush: true)
                    //result.put(ResultJson.DATA.name,params?.html)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def update() {
        def result = ResultJson.resultJson()

        def updator = params?.userNo
        def name = params.name?:"New Widget"
        def html = params.html
        def classFlag = params.classFlag
        try {
            def id = Integer.parseInt(params?.id)
            XdWidget data = XdWidget.get(id)
            XdWidget old = XdWidget.findByName(name)
            if(!data){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else if(old && old.id != data.id) {
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else{
                data.setName(name)
                data.setHtml(SysCommUtil.String2Clob(html))
                data.setUpdator(updator)
                data.setClassFlag(classFlag)
                data.save(flush: true)
                result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    /**
     * 新增Widget
     * @return
     */
    @Override
    def add() {

        def result = ResultJson.resultJson()

        def userNo = params?.userNo
        def name = params.name?:"New Widget"
        def html = params.html?:"<input class='layui-input'>"
        def classFlag = params?.classFlag
        try {
            XdWidget data = XdWidget.findByName(name)
            if(data){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                data = XdWidget.newInstance()
                data.setName(name)
                data.setHtml(SysCommUtil.String2Clob(html))
                data.setCreator(userNo)
                data.setClassFlag(classFlag)
                data.save(flush: true)
                result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        def result = ResultJson.resultJson()
        def id = Long.parseLong(params?.id)
        def data =  XdWidget.get(id)
        result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put("domId","${id}-${SysCommUtil.milliseconds()}")
        render result
    }

    @Override
    def query() {
        def result = ResultJson.resultJson()
        def offset = Integer.parseInt(params.page?:"1")
        def max = Integer.parseInt(params.limit?:"10")
        def userNo = params?.userNo
        def searchStr =  params.searchStr?String.valueOf(params.searchStr):""
        def criteria = XdWidget.createCriteria()
        def data = criteria.list([offset:offset-1,max:max] ){
            if(searchStr  ){
                if(searchStr.indexOf("'") ==-1){
                    like("name","%${searchStr}%")
                }
            }
        }
        def count =  XdWidget.countByNameLike("%${searchStr}%")

        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put(ResultJson.DATA.name,SysCommUtil.copyMapList(data))
        result.put(ResultJson.COUNT.name,count)

        render result
    }

    @Override
    def delete() {
        def result = ResultJson.resultJson()

        try {
            def id = Integer.parseInt(params?.id)
            def userNo = params?.userNo
            def user = SysUsers.findByUserNo(userNo)
            def userGrade = authUtilsService.isAvailable(user)
            XdWidget data = XdWidget.get(id)
            if(!data){
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }else{
                if(SysInitData.USER_GRADE_ADMIN_FUNC.contains(userGrade)){
                    data.delete(flush: true)
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }else{
                    result.put(ResultJson.CODE.name,ResultCode.OPT_NOT_AUTH.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_NOT_AUTH.message)
                }
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    /**
     * xdWidget-清单-下拉框
     * @return
     */
    def xdWidgets(){
        def result = ResultJson.resultJson()
        def xdWidgets = XdWidget.list()
        def xdWidget=[]
        def panelFlag =Boolean.parseBoolean(params.panelFlag?:"false")
        xdWidgets?.each {
            def data =[name: it.name,value: it.id]
            if(panelFlag){
                if( it.classFlag.indexOf("panel")!=-1){
                    xdWidget.add(data)
                }
            }else{
                if( it.classFlag.indexOf("panel") == -1 ){
                    xdWidget.add(data)
                }
            }
        }
        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put(ResultJson.DATA.name,xdWidget)
        //println result
        render result
    }
    /**
     * 获取所有已配置的widget清单
     * @return
     */
    def listXdWidget(){
        def result = ResultJson.resultJson()
        def offset = Integer.parseInt(params.page?:"1")
        def max = Integer.parseInt(params.limit?:"10")
        //def userNo = params?.userNo
        //def searchStr = params?.searchStr
        //def panelId = params.panelId
        def data = sysDBUtilService.pageResult(SysAuthSQL.getWidgetSQL(params),offset,max,sysDBUtilService.getGroovySqlInstance())
        def count = sysDBUtilService.pageCount(SysAuthSQL.getWidgetSQL(params),sysDBUtilService.getGroovySqlInstance())

        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put(ResultJson.DATA.name,data)
        result.put(ResultJson.COUNT.name,count)

        render result
    }


    /**
     * 获取所有已配置的widget清单
     * @return
     */
    def listXdWidgetPanel(){
        def result = ResultJson.resultJson()
        def offset = Integer.parseInt(params.page?:"1")
        def max = Integer.parseInt(params.limit?:"10")
        //def userNo = params?.userNo
        def searchStr = params?.searchStr
        def data = sysDBUtilService.pageResult(SysAuthSQL.getWidgetPanelSQL(searchStr),offset,max,sysDBUtilService.getGroovySqlInstance())
        def count = sysDBUtilService.pageCount(SysAuthSQL.getWidgetPanelSQL(searchStr),sysDBUtilService.getGroovySqlInstance())

        result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
        result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
        result.put(ResultJson.DATA.name,data)
        result.put(ResultJson.COUNT.name,count)

        render result
    }

    /**
     * 新增widget
     * @return
     */
    def addXdWidget(){
        def result = ResultJson.resultJson()

        def userNo = params?.userNo

        def xdWidgetId = params?.xdWidgetId
        XdWidget data = XdWidget.get(Long.parseLong(xdWidgetId))
        def classFlag = data.classFlag
        switch (classFlag) {
            case "xd-input"://输入框
                 addWidgetInput()
                break;
            case "xd-select"://下拉框
                 addWidgetSelect()
                break;
            case "xd-date"://日期
                 addWidgetDate();
                break;
            case "xd-panel-table":
                addWidgetPanel();
                break;

        }
    }

    /**
     *新增Panel
     * @return
     */
    def addWidgetPanel(){
        def result = ResultJson.resultJson()
        def userNo = params?.userNo
        def name = params.name?:"New Panel"

        try {
            def xdWidgetId = Long.parseLong(params?.xdWidgetId)
            def label = params?.domId
            XdWidgetPanel data = XdWidgetPanel.findByName(name)
            if(data){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                data = XdWidgetPanel.newInstance()
                data.setName(name)
                data.setLabel(label)
                data.setXdWidgetId(xdWidgetId)
                data.setHtml(SysCommUtil.String2Clob(params?.html))
                data.setCreator(userNo)
                data.save(flush: true)

                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    /**
     * 新增input
     * @return
     */
    def addWidgetInput(){
        def result = ResultJson.resultJson()
        def userNo = params?.userNo
        def name = params.name?:"New Input"
        def xdWidgetId =Long.parseLong(params?.xdWidgetId)
        //def xdWidget = XdWidget.get(xdWidgetId)
        try {
            XdWidgetInput data = XdWidgetInput.findByName(name)
            if(data){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                data = XdWidgetInput.newInstance()
                data.setName(name)
                data.setLabel(name)
                data.setXdWidgetId(xdWidgetId)
                //data.setHtml(xdWidget.getHtml())
                data.setCreator(userNo)
                data.save(flush: true)
                //result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }

    }

    def addWidgetDate(){
        def result = ResultJson.resultJson()

        def userNo = params?.userNo
        def name = params.name?:"New Date"
        def xdWidgetId =Long.parseLong(params?.xdWidgetId)
        //def xdWidget = XdWidget.get(xdWidgetId)
        try {
            XdWidgetDate data = XdWidgetDate.findByName(name)
            if(data){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                data = XdWidgetDate.newInstance()
                data.setName(name)
                data.setLabel(name)
                data.setXdWidgetId(xdWidgetId)
                data.setCreator(userNo)
                //data.setHtml(xdWidget.getHtml())
                data.save(flush: true)

                //result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }

    def addWidgetSelect(){
        def result = ResultJson.resultJson()

        def userNo = params?.userNo
        def name = params.name?:"New Select"
        def xdWidgetId = Long.parseLong(params?.xdWidgetId)
        //def xdWidget = XdWidget.get(xdWidgetId)
        try {
            XdWidgetSelect data = XdWidgetSelect.findByName(name)
            if(data){
                result.put(ResultJson.CODE.name,ResultCode.EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.EXISTS_ERROR.message)
            }else {
                data = XdWidgetSelect.newInstance()
                data.setName(name)
                data.setLabel(name)
                data.setXdWidgetId(xdWidgetId)
                data.setCreator(userNo)
                //data.setHtml(xdWidget.getHtml())
                data.save(flush: true)
               // result.put(ResultJson.DATA.name,SysCommUtil.copyMap(data))
                result.put(ResultJson.CODE.name,ResultCode.OPT_SUCCESS.code)
                result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render result
        }
    }


}
